package org.jeecg.modules.zixishi.service.impl;

import org.jeecg.modules.zixishi.entity.Zixishi;
import org.jeecg.modules.zixishi.entity.SwiperPic;
import org.jeecg.modules.zixishi.entity.Quyu;
import org.jeecg.modules.zixishi.mapper.SwiperPicMapper;
import org.jeecg.modules.zixishi.mapper.QuyuMapper;
import org.jeecg.modules.zixishi.mapper.ZixishiMapper;
import org.jeecg.modules.zixishi.service.IZixishiService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 自习室
 * @Author: jeecg-boot
 * @Date:   2024-05-19
 * @Version: V1.0
 */
@Service
public class ZixishiServiceImpl extends ServiceImpl<ZixishiMapper, Zixishi> implements IZixishiService {

	@Autowired
	private ZixishiMapper zixishiMapper;
	@Autowired
	private SwiperPicMapper swiperPicMapper;
	@Autowired
	private QuyuMapper quyuMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Zixishi zixishi, List<SwiperPic> swiperPicList,List<Quyu> quyuList) {
		zixishiMapper.insert(zixishi);
		if(swiperPicList!=null && swiperPicList.size()>0) {
			for(SwiperPic entity:swiperPicList) {
				//外键设置
				entity.setSourceId(zixishi.getId());
				swiperPicMapper.insert(entity);
			}
		}
		if(quyuList!=null && quyuList.size()>0) {
			for(Quyu entity:quyuList) {
				//外键设置
				entity.setZixishiId(zixishi.getId());
				quyuMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Zixishi zixishi,List<SwiperPic> swiperPicList,List<Quyu> quyuList) {
		zixishiMapper.updateById(zixishi);
		
		//1.先删除子表数据
		swiperPicMapper.deleteByMainId(zixishi.getId());
		quyuMapper.deleteByMainId(zixishi.getId());
		
		//2.子表数据重新插入
		if(swiperPicList!=null && swiperPicList.size()>0) {
			for(SwiperPic entity:swiperPicList) {
				//外键设置
				entity.setSourceId(zixishi.getId());
				swiperPicMapper.insert(entity);
			}
		}
		if(quyuList!=null && quyuList.size()>0) {
			for(Quyu entity:quyuList) {
				//外键设置
				entity.setZixishiId(zixishi.getId());
				quyuMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		swiperPicMapper.deleteByMainId(id);
		quyuMapper.deleteByMainId(id);
		zixishiMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			swiperPicMapper.deleteByMainId(id.toString());
			quyuMapper.deleteByMainId(id.toString());
			zixishiMapper.deleteById(id);
		}
	}
	
}
